
// Query
function query(query,functionName) {
	query = Object.toJSON(query);
	startTime = microtime();
	new Ajax.Request("./ajaxServer.php", {
		method: 'post',
		parameters: {"query":query},
		onSuccess: function(data) {
			endTime = microtime();
			duration(endTime-startTime);
			functionName(new label(data.responseText.evalJSON(true)));
		}
	});
}

function writeResultOnTextarea(label) {
	$('result').value = Object.toJSON(label.getData());
}

function microtime () {
    return new Date().getTime();
}

function duration(duration) {
	$('duration').innerHTML = 'Duration : '+duration+' ms';
}

var ZzIO = Class.create({
	initialize : function(id) {
		this.arrayQuery = {};
		this.arrayQuery['id'] = id;
	},
	
	get : function(val) {
		if(!this.arrayQuery['getVal'])
			this.arrayQuery['getVal'] = [];
		this.arrayQuery['getVal'].push(val);
	},
	
	set : function(val,value) {
		if(!this.arrayQuery['setVal'])
			this.arrayQuery['setVal'] = {};
		this.arrayQuery['setVal'][val] = value;
	},
	
	getChildren : function(type) {
		if(!this.arrayQuery['getChildren'])
			this.arrayQuery['getChildren'] = {};
		if(type)
			this.arrayQuery['getChildren']['type'] = type;
		this.arrayQuery['getChildren']['numPage'] = 0;
		this.arrayQuery['getChildren']['nbChildrenPerPage'] = 'noLimit';
		this.arrayQuery['getChildren']['order'] = 'normal';
	},
	
	numPage : function(numPage) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		this.arrayQuery['getChildren']['numPage'] = numPage;
	},
	
	nbChildren: function(nbChildren) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		this.arrayQuery['getChildren']['nbChildrenPerPage'] = nbChildren;
	},
	
	childrenOrder: function(childrenOrder) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		this.arrayQuery['getChildren']['childrenOrder'] = childrenOrder;
	},
	
	getChildrenValue: function(values) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		this.arrayQuery['getChildren']['val'] = values;
	},
	
	defCriteria: function(ref,operator,val) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		if(!this.arrayQuery['getChildren']['criteria'])
			this.arrayQuery['getChildren']['criteria'] = [];
		this.arrayQuery['getChildren']['criteria'].push({'ref':ref,'operator':operator,'value':val});
	},
	
	setRef: function(ref,val) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		if(!this.arrayQuery['getChildren']['ref'])
			this.arrayQuery['getChildren']['ref'] = [];
		this.arrayQuery['getChildren']['ref'].push({'ref':ref,'value':val});
	},
	
	setUserRef: function(ref,val) {
		if(!this.arrayQuery['getChildren'])
			this.getChildren();
		if(!this.arrayQuery['getChildren']['userRef'])
			this.arrayQuery['getChildren']['userRef'] = [];
		this.arrayQuery['getChildren']['userRef'].push({'ref':ref,'value':val});
	},
	
	getQuery : function() {
		alert(Object.toJSON(this.arrayQuery));
	},
	
	execQuery : function(functionName) {
		if(!functionName)
			functionName=writeResultOnTextarea;
		query(this.arrayQuery,functionName);
	}

});

var label = Class.create({
	initialize : function(data) {
		this.data = data;
	},
	
	get : function(val) {
		if(!this.data['value'][val])
			return;
		return this.data['value'][val];
	},
	
	getChildren : function(val) {
		if(!this.data['children'])
			return;
			var i =0;
		var children = [];
		for (child in this.data['children']) {
			labelChild = {};
			listChildren = this.data['children'];
			labelChild['id'] = child;
			labelChild['value'] = listChildren[child];
			children.push(new label(labelChild));
		}
		return children;
	},
	
	getData : function() {
		return this.data;
	}
	

});

function test() {

	var io = new ZzIO(1);

	io.get('name');

	//io.set('name','new name');
	//io.set('name2','new name');
	
	io.getChildren();
	io.getChildrenValue(['name']);
	io.numPage(0);
	io.nbChildren(10);
	io.childrenOrder('normal');
	io.defCriteria('test','>=',0);
	
	io.execQuery();

	
}

function include(src) {
	var header = document.getElementsByTagName("head" );
	var script = document.createElement("script" );
	script.setAttribute("type","text/javascript");
	script.setAttribute("src","./ZzScript/"+src);
	header[0].appendChild(script);
}

function openWindowQuery() {
	Dialog.alert("<textarea id=\"query\" style=\"width: 400px; height: 200px;\">var io = new ZzIO(1);\n\nio.get('name');\n\n//io.set('name','new name');\n//io.set('name2','new name');\n\nio.getChildren('article');\nio.getChildrenValue(['name']);\nio.numPage(0);\nio.nbChildren(10);\nio.childrenOrder('normal');\nio.defCriteria('test','>=',0);\n\nio.execQuery();</textarea><textarea id=\"result\" style=\"width: 400px; height: 200px;\"></textarea><br/><button onclick=\"eval($('query').value);\">Execute !</button><br/><p id=\"duration\" style=\"margin: 0; padding: 0;\">&nbsp;</p>", 
		{top: 10, width:500, className: "alphacube", okLabel: "Close"})    
}

